home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 09 / resclok2 / resclok2.c next >
Text File  |  1988-07-11  |  4KB  |  170 lines

  1. /*
  2.       Module name.
  3.         resclok2.c - display clock w/optional timer
  4.  
  5. -)    Functional description.
  6.          This program will display a clock in the upper right corner
  7.          of the screen and optionally accept a time and DOS command
  8.          as an argument.  At the specified time the command is passed
  9.          to DOS and the TSR is terminated and freed from memory.  The
  10.          command length is limited to 15 characters, so batch files
  11.          should be used to execute more complex command line
  12.          requirments
  13.  
  14. --)   NOTICE:  Copyright (C) 1988 Essential Software, Inc.
  15. */
  16. #include "stdio.h"
  17. #include "tsr.h"
  18. #include    "dos.h"
  19.  
  20. #define  TRUE  1
  21. #define  FALSE 0
  22. #define    SCAN_Q 16    /* scan code of Q key */
  23.  
  24. #define  ROW   0
  25. #define  COL   65
  26. #define  ATTR  0x70   /* black on white */
  27.  
  28. /* int 16h unique function we use to see if loaded */
  29. unsigned int myfunc = 0x7272;
  30.  
  31. int hr,min,sec,hd;
  32. int schr, schm;       /* scheduled time vars */
  33. int  cmdsize;
  34. char cmd[129];
  35. unsigned char stop = 0;
  36.  
  37. main(argc,argv)
  38. int  argc;
  39. char **argv;
  40. {
  41.    void clock();
  42.    int  add_arg;      /* counter for command line arguments */
  43.  
  44.    /* process timed command request */
  45.    if(argc > 2) {
  46.       tim_pars(argv[1]);               /* parse the time into schr & schm */
  47.  
  48.       add_arg = 2;
  49.       while(argc-- > 2) {              /* add all arguments to command line */
  50.          strcat(cmd, argv[add_arg++]);
  51.          strcat(cmd, " ");
  52.       }
  53.       strcat(cmd, "\r\n");             /* cause return at end of command */
  54.    }
  55.  
  56.    if (tsrloaded(myfunc) == FALSE)
  57.       {
  58.         printf("RESCLOK2  By Essential Software\n");
  59.  
  60.       if((cmdsize = strlen(cmd)) > 0)      /* verify command and time */
  61.          if(cmdsize <= 15)
  62.             printf("At %02d:%02d Execute %s", schr, schm, cmd);
  63.          else
  64.             printf("Command length exceeds max of 15");
  65.  
  66.        inittsr2(CTRL_KEY | ALT_KEY,SCAN_Q, clock, 0, myfunc, SIG);
  67.       }
  68.    else
  69.       {
  70.       printf("RESCLOK2 Already Loaded!\n");
  71.       exit(1);
  72.       }
  73. }
  74.  
  75.  
  76.    static int cnt = 99;       /*    initialize second comparison variable */
  77.  
  78. void clock()
  79. {
  80.    char curtime[15];          /* buffer to hold time */
  81.    int  loop;
  82.     union REGS regs;                /* register structure for dos call */
  83.     extern int _hotkey_hit;        /* set to 1 if hotkey was hit */
  84.  
  85.     if(_hotkey_hit){
  86.         if(freetsr() == 1){
  87.          colrprts("Not Loaded Last! - TSR Will Still Be Loaded!",0,7);
  88.             _hotkey_hit = 0;        /* reset hotkey flag */
  89.         }
  90.     }            
  91.  
  92.  
  93.     regs.h.ah = 0x2c;        /* get system time with DOS call */
  94.     intdos(®s,®s);    
  95.     sec = regs.h.dh;
  96.  
  97.    /* we'll only update the clock display when sec changes */
  98.    if(cnt != sec) {
  99.       cnt = sec;
  100.         hr = regs.h.ch;
  101.         min = regs.h.cl;
  102.  
  103.       /* pretty up display with leading and trailing space */
  104.       curtime[0] = ' ';
  105.       strtime(&curtime[1], hr,min,sec,0,9);
  106.       strcat(curtime, " ");
  107.  
  108.       display(curtime);                /* put the time on the screen */
  109.    }
  110.  
  111.    /* process command if one was requested */
  112.       if (!stop)
  113.          if(*cmd && hr == schr && min == schm)
  114.             {
  115.             if (freetsr() == 1)           /* free ISR when done */
  116.                {
  117.                stop = 1;
  118.                colrprts("Not Loaded Last! - TSR Will Still Be Loaded!",0,7);
  119.                }
  120.             else
  121.                if(cmdsize <= 15)
  122.                   kybdstuf(cmd);     /* place the command in the keyboard buffer */
  123.                else
  124.                   colrprts("Command Greater Than 15 Characters!", 0xc, 0);
  125.  
  126.             }
  127. }
  128.  
  129. /* Interleave atribute and display time */
  130. display(str)
  131. char *str;
  132. {
  133.    register char *curptr;   /* used to steo thru buffer */
  134.    char buf[31];            /* actual buffer to write to screen */
  135.  
  136.  
  137.    curptr = buf;
  138.  
  139.    /* merge char and attribute for screen format */
  140.    while(*str) {
  141.       *curptr++ = *str++;
  142.       *curptr++ = ATTR;
  143.       }
  144.  
  145.    /* do the actual display */
  146.    memtoscr(10, (ROW * 160) + (COL * 2), buf);
  147.    return;
  148. }
  149.  
  150. /*
  151.    This function takes the time in HH:MM format and places it into
  152.    two int variables for hour and minute.
  153. */
  154. tim_pars(str)
  155. char *str;
  156. {
  157.    char *ptr;   /* used to step thru str */
  158.  
  159.    for(ptr = str; *ptr != ':' && *ptr; ptr++);
  160.  
  161.    *ptr++ = '\0';                      /* split hour and point to minute */
  162.    schr = atoi(str);
  163.    schm = atoi(ptr);
  164.  
  165.    return(0);
  166. }
  167.  
  168.  
  169.                     
  170.